package com.jsgt.greekfire.ui.mine.coindetail.gfctransfer

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.AssetsBean
import com.jsgt.greekfire.common.util.MD5Util
import com.jsgt.greekfire.factory.CoinViewModelFactory
import com.jsgt.greekfire.viewmodel.CoinViewModel
import kotlinx.android.synthetic.main.activity_gfc_transfer.*


/**
 * GFC划转
 */
class GFCTransferActivity : BaseActivity() {


    private var assetsBean: AssetsBean? = null

    companion object {
        private const val REQUEST_CODE = 1001
        private const val REQUEST_CODE_SCAN = 1002
        const val ADDRESS_BEAN = "address_bean"
        private const val ASSETS_BEAN = "bean"
        fun start(context: Context, assetsBean: AssetsBean) {
            val intent = Intent(context, GFCTransferActivity::class.java)
            intent.putExtra(ASSETS_BEAN, assetsBean)
            context.startActivity(intent)
        }
    }


    private val coinViewModel: CoinViewModel by lazy {
        ViewModelProvider(
            this,
            CoinViewModelFactory()
        ).get(CoinViewModel::class.java)
    }


    private val handler: Handler = Handler()

    /**
     * 延迟线程，看是否还有下一个字符输入
     */
    private val delayRun = Runnable { //在这里调用服务器的接口，获取数据

        val walletName = transfer_number.text.toString().trim()

        if (!TextUtils.isEmpty(walletName) && walletName.toDouble() > assetsBean!!.assetBalance!!) {
            ToastUtils.show(this, "数量不能大于余额")
        }
    }


    override fun getLayoutId(): Int = R.layout.activity_gfc_transfer

    override fun main() {

        assetsBean = intent.getParcelableExtra(GFCTransferActivity.ASSETS_BEAN)
        title = "GFC划转"



        transfer_number.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
//                if (delayRun != null) {
//                    //每次editText有变化的时候，则移除上次发出的延迟线程
//                    handler.removeCallbacks(delayRun)
//                }

                //延迟800ms，如果不再输入字符，则执行该线程的run方法
//                handler.postDelayed(delayRun, 800)
                handler.post(delayRun)
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

        })

        confirm_tv.setOnClickListener {

            if (TextUtils.isEmpty(transfer_number.text.toString().trim())) {
                ToastUtils.show(this, "数量不能为空")
                return@setOnClickListener
            }



            if (TextUtils.isEmpty(transfer_password.text.toString().trim())) {
                ToastUtils.show(this, "请输入交易密码")
                return@setOnClickListener
            }


            coinViewModel.convert(
                assetsBean!!.assetBalance.toString(),
                transfer_number.text.toString().trim(),
                MD5Util.getMD5(transfer_password.text.toString().trim())
            )
            hideKeyboard(confirm_tv)
        }


        coinViewModel.convertLiveData.observe(this, Observer {
            showProgress(false)
            finish()
        })

        coinViewModel.errorLiveData.observe(this, Observer {
            showProgress(false)
        })


    }
    fun hideKeyboard(view: View) {
        val imm = view.getContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm?.hideSoftInputFromWindow(view.getWindowToken(), 0)
    }
}